/*
 * @(#) $Header$
 *
 * Copyright (C) 2011  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util.comparator;

import java.util.Comparator;

/**
 * Class {@code ReverseComparator} reverse the comparison of the adapted
 * comparator.
 *
 * @param   <T>   the type being compared.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.comparator.ReverseComparator">Daniel Léonard</a>
 * @version $Revision$
 */
public class ReverseComparator<T> implements Comparator<T> {

//---------------------------
// Instance variables
//---------------------------

   /** The adapted comparator. */
   private Comparator<T> comparator;


//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    * @param   comparator   the adapted comparator.
    */
   public ReverseComparator(Comparator<T> comparator) {
      this.setComparator(comparator);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Changes the adapted comparator.
    * @param   comparator   the new comparator.
    */
   protected void setComparator(Comparator<T> comparator) {
      this.comparator = comparator;
      }

   /**
    * Gets the adapted comparator.
    * @return   the comparator.
    */
   protected Comparator<T> getComparator() {
      return this.comparator;
      }


//---------------------------
// Implemented methods from java.util.Comparator
//---------------------------

   /**
    * Reverse compare the two objects.
    * @param   t1   the first object.
    * @param   t2   the second object.
    * @return   a negative integer if t2 is smaller than t1, zero if they are
    *           equals and a positive integer if t2 is greater than t1.
    */
   @Override
   @SuppressWarnings("hiding")
   public int compare(T t1, T t2) {
      Comparator<T> comparator = this.getComparator();
      int comparison = comparator.compare(t2, t1);
      return comparison;
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Creates a new reverse comparator.
    * @param   comparator   the adapted comparator.
    * @param   <T>   the type being compared.
    * @return   a reverse comparator.
    */
   public static <T> ReverseComparator<T> create(Comparator<T> comparator) {
      ReverseComparator<T> reverse = new ReverseComparator<>(comparator);
      return reverse;
      }

   }
